﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Warehouse.Web.AppCode;

namespace Warehouse.Web.Sales
{
    public partial class BillList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString.AllKeys.Contains("search"))
                {
                    DateTime dtStart = DateTime.Parse(Request.QueryString["Start"]);
                    DateTime dtEnd = DateTime.Parse(Request.QueryString["End"]);

                    this.tbStartDate.Text = dtStart.ToString("yyyy-MM-dd");
                    this.tbEndDate.Text = dtEnd.ToString("yyyy-MM-dd");

                    var distributors = DataAccess.Distributor.GetDistributors().ToDictionary(p => p.ID);
                    var houses = DataAccess.Warehouse.GetWarehouses().ToDictionary(p => p.ID);

                    List<DataWrapper> datasource = new List<DataWrapper>();
                    datasource.AddRange(DataAccess.SalesBill.GetList(p => p.SalesDate >= dtStart.Date && p.SalesDate < dtEnd.Date.AddDays(1)).Select(p => new DataWrapper()
                    {
                        ID = p.ID,
                        Date = p.SalesDate,
                        SerialNumber = p.SerialNumber,
                        Status = p.Status,
                        Summary = p.Summary,
                        SupplierName = distributors[p.DistributorID].Name,
                        HouseName = houses[p.HouseID].Name,
                        TypeName = GetTypeName(p.BillType),
                        Type = p.Type,
                        CreateDate = p.CreateDate
                    }).ToArray());

                    datasource.AddRange(DataAccess.ReceiptBill.GetList(p => p.ReceiptDate >= dtStart.Date && p.ReceiptDate < dtEnd.Date.AddDays(1) && !p.SalesBillID.HasValue).Select(p => new DataWrapper()
                    {
                        ID = p.ID,
                        Date = p.ReceiptDate,
                        SerialNumber = p.SerialNumber,
                        Status = p.Status,
                        Summary = p.Summary,
                        SupplierName = distributors[p.DistributorID].Name,
                        HouseName = string.Empty,
                        TypeName = p.IsExpense ? "付款单" : "收款单",
                        Type = p.IsExpense ? -2 : -1,
                        CreateDate = p.CreateDate
                    }).ToArray());


                    this.rptBills.DataSource = datasource.OrderBy(p => p.CreateDate).OrderBy(p => p.Date);
                    this.rptBills.DataBind();
                }
                else
                {
                    this.tbStartDate.Text = DateTime.Now.AddDays(1 - DateTime.Now.Day).ToString("yyyy-MM-dd"); ;
                    this.tbEndDate.Text = DateTime.Now.ToString("yyyy-MM-dd");
                }
            }
        }

        class DataWrapper
        {
            public Guid ID { get; set; }
            public DateTime Date { get; set; }
            public string SerialNumber { get; set; }
            public string SupplierName { get; set; }
            public int Status { get; set; }
            public string Summary { get; set; }
            public string HouseName { get; set; }
            public string TypeName { get; set; }
            public int Type { get; set; }
            public DateTime CreateDate { get; set; }
        }

        string GetTypeName(DataAccess.SalesBillType tp)
        {
            switch (tp)
            {
                case Warehouse.DataAccess.SalesBillType.Return:
                    return "退货单";
                case Warehouse.DataAccess.SalesBillType.Sales:
                    return "销售单";
                case Warehouse.DataAccess.SalesBillType.Gift:
                    return "赠品单";
                default:
                    return string.Empty;
            }
        }
    }
}
